//
//  YTAddWithdrawAccountViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/26.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTAddWithdrawAccountViewController.h"
#import "YTAccountWithdrawViewController.h"
#import <CocoaSecurity/CocoaSecurity.h>
#import "YTUserProfileServices.h"

@interface YTAddWithdrawAccountViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *tipText;
@property (weak, nonatomic) IBOutlet UILabel *accountText;

@property (weak, nonatomic) IBOutlet UITextField *nameText;
@property (weak, nonatomic) IBOutlet UITextField *accountNumText;
@property (weak, nonatomic) IBOutlet UITextField *bankNameText;
@property (weak, nonatomic) IBOutlet UIButton *completeBtn;
@end

@implementation YTAddWithdrawAccountViewController

- (void)initialize {
    _tipText.text = [NSString stringWithFormat:@"请绑定本人%@", _account];
    _accountNumText.placeholder = [NSString stringWithFormat:@"本人%@号", _account];
    self.navigationItem.title = [NSString stringWithFormat:@"添加%@", _account];
    _accountText.text = [_account isEqualToString:@"银行卡"] ? @"卡号" : _account;
}

- (void)bindViewModel {
    
    id (^validate)(NSString *value) = ^id(NSString *value) {
        return @(value.length >= 2);
    };
    
    RACSignal *nameSignal = [[_nameText.rac_textSignal distinctUntilChanged] map:validate];
    RACSignal *accountSignal = [[_accountNumText.rac_textSignal distinctUntilChanged] map:^id(NSString *value) {
        return @(value.length >= 6);
    }];
    RACSignal *banckSignal = [[_bankNameText.rac_textSignal distinctUntilChanged] map:validate];
    RAC(_completeBtn, enabled) = [RACSignal combineLatest:@[nameSignal, accountSignal, banckSignal]
                                                   reduce:^id(NSNumber *name, NSNumber *account, NSNumber *bank){
        if ([self.type isEqualToString:@"3"]) {
            return @(name.boolValue && account.boolValue && bank.boolValue);
        } else {
            return @(name.boolValue && account.boolValue);
        }
    }];
    RAC(_completeBtn, backgroundColor) = [RACObserve(_completeBtn, enabled) map:^id(NSNumber *value) {
        return value.boolValue ? kMainBlueColor : [UIColor lightGrayColor];
    }];
}

- (IBAction)addAccountAction:(UIButton *)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入提现密码" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 1) return;
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
    dataDict[@"type"] = _type;
    dataDict[@"name"] = _nameText.text;
    dataDict[@"number"] = _accountNumText.text;
    dataDict[@"bank"] = _bankNameText.text.length != 0 ? _bankNameText.text : @"";
    NSString *userID = [YTUtilityManager manager].userModel.userId;
    NSString *passwrod = [NSString stringWithFormat:@"%@%@paypassword", [alertView textFieldAtIndex:0].text, userID];
    NSString *passwordMD5 = [CocoaSecurity md5:passwrod].hex;
    NSString *paySum = [NSString stringWithFormat:@"addAccount%@%@%@%@yitu8.net", _nameText.text, _accountNumText.text, userID, passwordMD5];
    dataDict[@"paySum"] = [CocoaSecurity md5:paySum].hex;
    [[YTUserProfileServices addWithdrawAccount:dataDict] subscribeNext:^(NSDictionary *response) {
        for (UIViewController *vc in self.navigationController.childViewControllers) {
            if ([vc isKindOfClass:[YTAccountWithdrawViewController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
            }
        }
        [SVProgressHUD showSuccessWithStatus:@"添加成功"];
    } error:^(NSError *error) {
        self.accountNumText.text = @"";
        self.bankNameText.text = @"";
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([_account isEqualToString:@"银行卡"]) {
        return 3;
    }
    return 2;
}

@end
